<?php
	/*
	** The Display class for the main content section of the site
	**
	** Copyright MONKeLABS 2011
	**
	** File History
	**
	** Version		Date Modified		Author		Description of changes
	** -------------------------------------------------------------------
	** 1.00			06/04/2011			Rob Davis	File Creation
	**
	**
	** TODO: Add functionality to server more than just HTML files in the content section
	** TODO: Add a file name tag to the nemu layout so that a two links can point
	** 		 to the same file.
	** TODO: abstract tag reaplceing in xslt file into class of its own
	** TODO: Make tage matching in xslt non case sesetive
	** TODO: make all CSS colours dymanic in PHP (write a colour mamanger class for them)
	*/

	require_once("constants.php");
		
	/*
	** The main engine calss for the
	** content control
	*/
	class engine
	{
		
		
		public function GetContent()
		{
			
			
			if(!isset($_GET[PAGE_PARAM]))
			{
				return "";	
			}
			
			$cPage = $_GET[PAGE_PARAM];
			
			if($cPage == "")
			{
				return "";	
			}else
			{
				return $this->ParseHTML(strtolower($cPage) . PAGE_EXTENTION);
			}
			
		}
		
		/*
		** Return the URL to the page
		*/
		private function GetServerURLFormatted()
		{
			return SERVER_PROTOCOL . $_SERVER[SERVER_NAME] . "/" . INDEX_PAGE . "?" . PAGE_PARAM . "=";
		}
				
		/*
		** This function makes sure the links point to the right place
		*/
		private function InsertServerURL($xsltFileName)
		{
			
			$pp = "nogo";
			
			if(isset($_GET[PAGE_PARAM]))
			{
				$pp = $_GET[PAGE_PARAM];	
			}
			
			$xsltStr = file_get_contents($xsltFileName);
			
			$xsltStr = str_replace(PAGE_TAG, $pp, $xsltStr);

			return str_replace(SERVER_TAG,$this->GetServerURLFormatted(), $xsltStr);
		}
				
	
		/*
		** Transforms a XML to HTML for dumping into the master page
		*/
		public function ParseTemplate($xmlFileName, $xsltFileName)
		{
						
			// Process the document
			try
			{
				$XML = new DOMDocument(); 
				$XML->load($xmlFileName);
				
				$xslt = new XSLTProcessor(); 
				$XSL = new DOMDocument();  
				
				$XSL->loadXML($this->InsertServerURL($xsltFileName), LIBXML_NOCDATA); 
				$xslt->importStylesheet($XSL); 
				
				$result = $xslt->transformToXML( $XML ); 				
			}
			catch(Exception $ex)
			{
				$result = "Error: " . $ex;
			}
			
			return $result;			
		}
		
		
		
		/*
		** Parses a HTML file for output to the page
		*/
		public function ParseHTML($htmlFile)
		{
			return str_replace(CONTENT_TAG, $this->InsertServerURL(TEMPLATE_DIRECTORY . "/" . $htmlFile), $this->InsertServerURL(TEMPLATE_DIRECTORY . "/" .CONTAINER_FILE));
		}
		
		
		
		
		
		
		
		
		
		
			
	}

?>